/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_wtc_mar_get_mbstring.c                           */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/25 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"

#include "../../../src/module/tuxedowtc/config.h"
#include <tuxedowtc/wtc_comm.h>
#include <apmtest_comm.h>
#include <tuxedowtc/wtc_mar.h>

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

/**
 * gb18030
 */
static void __case1(void)
{
    char *p_bin = (char *) "68656c6c6fcac0bde7";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    char *p_str = NULL;
    unsigned int ui_str_len = 0;
    CU_ASSERT_EQUAL(wtc_mar_get_mbstring((char *) "gb18030", arr_payload, strlen(p_bin) / 2, &p_str, &ui_str_len), FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_str, "hello世界");
    wtc_free((void **) &p_str);
}

/**
 * gbk
 */
static void __case2(void)
{
    char *p_bin = (char *) "61d6d0cec462";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    char *p_str = NULL;
    unsigned int ui_str_len = 0;
    CU_ASSERT_EQUAL(wtc_mar_get_mbstring((char *) "gbk", arr_payload, strlen(p_bin) / 2, &p_str, &ui_str_len), FR_SUCC);
    CU_ASSERT_STRING_EQUAL(p_str, "a中文b");
    wtc_free((void **) &p_str);
}

int add_suite_wtc_mar_get_mbstring(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" wtc_mar_get_mbstring()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
